package com.xindaoapp.happypet.activity;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.view.PullToRefreshListView;

public class VideoActivity extends UMBaseActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_video);
        PullToRefreshListView listView = ((PullToRefreshListView) findViewById(R.id.listview));
        listView.addHeaderView(getHeaderView());
        listView.setAdapter(new T());
    }

    final MediaPlayer mediaPlayer = new MediaPlayer();

    private View getHeaderView() {
        View view = LayoutInflater.from(getBaseContext()).inflate(R.layout.item_video, null);
        final SurfaceView surfaceView = (SurfaceView) view.findViewById(R.id.surfaceView);

        DisplayMetrics displaymetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        LinearLayout.LayoutParams layoutParams =
                new LinearLayout.LayoutParams(displaymetrics.widthPixels,
                        displaymetrics.widthPixels);
        surfaceView.setLayoutParams(layoutParams);

        // 设置SurfaceView自己不管理的缓冲区
        surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        view.setTag(true);
        surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {}

            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                play(mediaPlayer, surfaceView);
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
        });
        return view;
    }

    private void play(final MediaPlayer mediaPlayer, SurfaceView surfaceView) {
        // 开始播放
        // if (!mediaPlayer.isPlaying()) {
        try {
            mediaPlayer.reset();
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            // 设置需要播放的视频
            mediaPlayer
                    .setDataSource("http://bbs.chinapet.com/source/plugin/leepet_thread/upload/201406/01/20140601102835538a8fd3d2fea.mp4");
            mediaPlayer.setLooping(true);
            // 把视频画面输出到SurfaceView
            mediaPlayer.setDisplay(surfaceView.getHolder());
            mediaPlayer.prepareAsync();
            mediaPlayer.setOnPreparedListener(new OnPreparedListener() {

                @Override
                public void onPrepared(MediaPlayer mp) {
                    // 播放
                    mediaPlayer.start();// 并直接从指定位置开始播放
                    mediaPlayer.seekTo(0);
                }
            });
            Toast.makeText(getBaseContext(), "开始播放！", Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            // TODO: handle exception
        }
    }

    class T extends BaseAdapter {
        private boolean isPlaying;

        @Override
        public int getCount() {
            return 12;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = LayoutInflater.from(getBaseContext()).inflate(R.layout.item_video2, null);
            return view;
        }

    }
}
